#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../my_codeh/display.h"
#include <sys/wait.h>
#include "../my_codeh/speech.h"
#include "../my_codeh/readdir.h"
#include "../my_codeh/play.h"
#include "../my_codeh/say_boot.h"
// 定义软件盒子页面对象
lv_obj_t *app_box_scr1 = NULL;
static lv_style_t style_bubble;
int jinxflag =0;
extern int music_flag;
extern char * result;
extern Node* p1;
extern LinkedList* list;

char* concatListToString(LinkedList* l) 
{
    // 链表为空判断
    if (!l || !l->first) {
        char* emptyStr = (char*)malloc(1);
        emptyStr[0] = '\0';
        return emptyStr;
    }

    // 用于统计总长度，方便分配内存
    int totalLen = 0;
    Node* p = l->first;
    do {
        totalLen += strlen(p->data) + 1;  // +1 预留换行符位置
        p = p->next;
    } while (p != l->first);

    // 分配足够内存存储拼接后的字符串（+1 用于存储结束符 '\0'）
    char* result = (char*)malloc(totalLen + 1);
    result[0] = '\0';  // 初始化空字符串

    int pos = 0;  // 记录当前拼接位置
    p = l->first;
    do {
        // 拼接当前节点字符串 + 换行符，返回写入的字符数（不含结束符）
        pos += snprintf(result + pos, totalLen - pos + 1, "%s\n", p->data);
        p = p->next;
    } while (p != l->first);
    result[totalLen] = '\0';  // 手动确保结束符（冗余处理，snprintf 会自动添加）
    return result;
}
static void create_app_icon(lv_obj_t *parent, app_item_t *app, int x, int y,int num)
{   
    // 图标容器(图标)
    lv_obj_t *icon_cont = lv_obj_create(parent);//以最开始的界面为父对象，在此基础建立四个图标
    lv_obj_set_size(icon_cont, 80, 80);  // 图标总尺寸
    lv_obj_set_pos(icon_cont, x, y);
    lv_obj_set_style_bg_opa(icon_cont, 0, 0);  // 透明背景
    lv_obj_set_style_border_opa(icon_cont, 0, 0);  // 无边框
    lv_obj_set_style_radius(icon_cont,10,0);//弧度圆角
    lv_obj_set_style_shadow_width(icon_cont,10,0);//点击后会产生阴影，产生反馈
    lv_obj_set_style_shadow_color(icon_cont,lv_color_hex(0x888888),0);//对应的颜色

    lv_obj_set_scrollbar_mode(icon_cont,LV_SCROLLBAR_MODE_OFF);
    lv_obj_clear_flag(icon_cont,LV_OBJ_FLAG_SCROLLABLE);//禁止滚动的代码

    lv_obj_set_style_bg_color(icon_cont,lv_color_hex(0xd0d0d0),LV_STATE_PRESSED);
    lv_obj_set_style_shadow_width(icon_cont,2,LV_STATE_PRESSED);//对饮阴影的宽度
    lv_obj_set_style_translate_y(icon_cont,2,LV_STATE_PRESSED);
    lv_obj_add_flag(icon_cont,LV_OBJ_FLAG_CLICKABLE);
    
    // 应用图标
    lv_obj_t *icon_img = lv_img_create(icon_cont);//以图标容器为父对象了
    lv_image_set_src(icon_img, app->icon_src);//输入对应的名字，结构体的作用
    lv_obj_set_size(icon_img, 80, 80);  // 图标大小
    lv_obj_align(icon_img, LV_ALIGN_TOP_MID, 0, -22);  // 图标居上   具体坐标还是做了一点调试
    switch (num)//通过这个来重复建立四个应用，根据num的不同来确定每一个不同的回调函数
    {
    case 6 : lv_obj_add_event_cb(icon_cont, app_icon_click6, LV_EVENT_CLICKED, NULL); break;
    }
    

    
    //应用名称
    lv_obj_t *icon_label = lv_label_create(parent);//后续补的名字 以白底为父对象
    lv_label_set_text(icon_label, app->name);//结构体的名字
    lv_obj_set_style_text_font(icon_label, &lv_font_montserrat_10, 0);
    int y1 = y+90;
    int x1 = x+10;
    lv_obj_set_pos(icon_label, x1, y1);//x y 是以图标的位置进行确定然后总体向下进行偏移调试确定位置



}
void init_bubble_style() 
{
    lv_style_init(&style_bubble);
    lv_style_set_bg_color(&style_bubble, lv_color_hex(0xE0F0FF)); // 背景色
    lv_style_set_bg_opa(&style_bubble, 255);                      // 不透明
    lv_style_set_radius(&style_bubble, 15);                       // 圆角
    lv_style_set_pad_all(&style_bubble, 15);                      // 内边距（控制气泡大小）
    lv_style_set_text_font(&style_bubble, &lv_font_montserrat_20); // 文字大小
    lv_style_set_text_color(&style_bubble, lv_color_hex(0x000000)); // 文字颜色
}
lv_obj_t *create_bubble(lv_obj_t *parent, int x, int y, const char *text) 
{
    lv_obj_t *bubble = lv_label_create(parent);
    lv_label_set_text(bubble, text);       // 设置文字（只改这里即可）
    lv_obj_add_style(bubble, &style_bubble, 0); // 复用通用样式
    lv_obj_set_pos(bubble, x, y);          // 固定位置（原位置）
    return bubble;
}
// 创建语音助手界面
void create_say_boot_screen()//一开始的初始父对象，在这上面建立四个图标和退出按钮
{   
    int screen_w = lv_disp_get_hor_res(NULL);  //以屏幕中心作为初始坐标，用来方便计算图标位置
    int screen_h = lv_disp_get_ver_res(NULL);  
    // 布局四个图标（2x2网格）
    int icon_gap = 60;  // 图标间距
    int start_x = (screen_w - (2 * 120 + icon_gap)) / 2;  // 水平居中起始X
    int start_y = (screen_h - (2 * 140 + icon_gap)) / 2;  // 垂直居中起始Y
    // 创建全屏白色背景父对象
    app_box_scr1 = lv_obj_create(lv_scr_act());//在开始就定义，方便作为全局变量调用
    lv_obj_set_size(app_box_scr1, lv_obj_get_width(lv_scr_act()), lv_obj_get_height(lv_scr_act()));
    lv_obj_set_style_bg_color(app_box_scr1, lv_color_hex(0xFFFFFF), 0);  // 白色背景
    lv_obj_set_style_border_opa(app_box_scr1, 0, 0);  // 无边框
    
    lv_obj_set_scrollbar_mode(app_box_scr1,LV_SCROLLBAR_MODE_OFF);
    lv_obj_clear_flag(app_box_scr1,LV_OBJ_FLAG_SCROLLABLE);//禁止滚动的代码
    lv_obj_t *icon_img = lv_img_create(app_box_scr1);//以图标容器为父对象了
    lv_image_set_src(icon_img, "A:/lingke/love.bmp");//输入对应的名字，结构体的作用
    lv_obj_set_size(icon_img, 200, 184);  // 图标大小
    lv_obj_set_pos(icon_img, 20, 100);  // 图标居上   具体坐标还是做了一点调试

    init_bubble_style();
    result =ctr_music_init();
    create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "Ni Hao"); 
    create_app_icon(app_box_scr1, &app_list[4], start_x + 360 + icon_gap, start_y-70,6);
    

}



void jinx_play()
{
    switch (jinxflag)
    {
    case 1: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "My Name is Jinx"); jinxflag=88; break;
    case 2: jinxflag=0; exit(0); break;
    case 3: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "Let's Play Piano!"); jinxflag=0; GetIdtoDo(1000);jinxflag=88; break;
    case 4: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "Let's Play Bomb!"); jinxflag=0; GetIdtoDo(1001);jinxflag=88; break;
    case 5: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "Let's Watch Video!"); jinxflag=0; GetIdtoDo(1002);jinxflag=88; break;
    case 6: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "Let's Play Wuzi!"); jinxflag=0; GetIdtoDo(1003);jinxflag=88; break;
    case 7: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 230, 180, "Let's Play Music!");
    ctr_music(list,8);jinxflag=88; break;
    case 8: create_bubble(app_box_scr1, 230, 180, 
"                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n                                                                                 \n ");
    create_bubble(app_box_scr1, 240, 180, result);jinxflag=88; break;
    case 9: ctr_music(list,9);jinxflag=88; break;
    case 10: ctr_music(list,10);jinxflag=88; break;
    case 11: ctr_music(list,5);jinxflag=88; break;
    case 12: ctr_music(list,6);jinxflag=88; break;
    case 13: ctr_music(list,7);jinxflag=88; break;
    }
}
static void app_icon_click6(lv_event_t *event) //
{   
    LV_LOG_USER("666\n"); 
    Automatic_Speech_Recognition(); 
    jinx_play();
}





